package com.yxcz.serviceImpl;

import java.util.ArrayList;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yxcz.dao.DocumentDAO;
import com.yxcz.dao.UserDAO;
import com.yxcz.model.Document;
import com.yxcz.service.DocumentService;

@Service
public class DocumentServiceImpl implements DocumentService {

	@Resource
	private DocumentDAO documentDAO;
	@Resource
	private UserDAO userDAO ;

	@Override
	public void add(String content, String name, Integer uid) throws Exception {
	  Document doc = new Document();
	  doc.setTitle(name);
	  doc.setUser(userDAO.find(uid));
	  doc.setContent(content);
	  doc.setCreatedate(new Date());
	  documentDAO.add(doc);	
	}

	@Override
	public Document get(int i) {
		return documentDAO.find(i);
	}

	@Override
	public ArrayList<Document> list() {
		return documentDAO.list();
	}

	@Override
	public void delete(Integer id) {
		
		documentDAO.delete(documentDAO.find(id));
	}

	@Override
	public void update(Document doc) {
		Document newdoc = documentDAO.find(doc.getId());
		newdoc.setContent(doc.getContent());
		newdoc.setTitle(doc.getTitle());
		documentDAO.save(newdoc);
	}

	
}
